package org.xx.armory.spring5;

import org.springframework.core.io.ByteArrayResource;

import java.nio.charset.StandardCharsets;

import static org.apache.commons.lang3.StringUtils.stripToEmpty;

/**
 * 表示一个文件的资源，文件内容是指定的字符串，并通过{@literal UTF8}编码。
 */
public class StringFileResource
        extends ByteArrayResource {
    private final String fileName;

    public StringFileResource(
            String fileName,
            String fileContent
    ) {
        this(fileName, fileContent, null);
    }

    public StringFileResource(
            String fileName,
            String fileContent,
            String description
    ) {
        super(fileContent != null ? fileContent.getBytes(StandardCharsets.UTF_8) : new byte[0], description);

        this.fileName = stripToEmpty(fileName);
    }

    @Override
    public String getFilename() {
        return this.fileName;
    }
}
